package com.hqyj.javaGraduation.hotel.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.javaGraduation.hotel.dao.UtMenuInfoDao;
import com.hqyj.javaGraduation.hotel.pojo.UtMenuInfo;
import com.hqyj.javaGraduation.hotel.service.UtMenuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

@Service
public class UtMenuInfoServiceImpl implements UtMenuInfoService {

    @Autowired
    UtMenuInfoDao utMenuInfoDao;

    //客户点菜
    @Override
    public HashMap<String, Object> insertMenuByUserId(UtMenuInfo utMenuInfo) {
        HashMap<String, Object> map = new HashMap<>();
        utMenuInfo.setMenuState("加入购物车");
        utMenuInfo.setUtInfoDate(new Date());
        int i = utMenuInfoDao.insertMenuByUserId(utMenuInfo);
        if(i>0){
            map.put("info","加入购物车成功");
        }
        else {
            map.put("info","加入购物车失败");
        }
        return map;
    }

    // 查询客户购物车信息
    @Override
    public HashMap<String, Object> queryAllMenuInfo(UtMenuInfo utMenuInfo) {
        HashMap<String, Object> map = new HashMap<>();
        //第一步：创建分页对象 userInfo.getPage()表示当前页，userInfo.getRow()每页大小
        Page<UtMenuInfo> utMenuInfoPage = new Page<>(utMenuInfo.getPage(),utMenuInfo.getRow());

        //创建查询条件对象
        QueryWrapper<UtMenuInfo> queryWrapper = new QueryWrapper<>();
        //精确查询
        queryWrapper.eq(utMenuInfo.getTableId()!=0, //条件
                "table_id",  //列名
                utMenuInfo.getTableId());   //参数值

        // 第二步: 查询分页，获取分页数据集
        Page<UtMenuInfo> pageList = utMenuInfoDao.selectPage(utMenuInfoPage,queryWrapper);

        //获取当前页的集合
        map.put("info",pageList.getRecords());
        //获取总条数
        map.put("total",pageList.getTotal());
        //获取总页数
        map.put("totalPage",pageList.getPages());
        return map;
    }

    // 客户加菜
    @Override
    public int addMenuByUtMenuInfo(UtMenuInfo utMenuInfo) {
        return utMenuInfoDao.addMenuByUtMenuInfo(utMenuInfo);
    }

    // 客户减少菜品订购数量
    @Override
    public int deleteMenuByUtMenuInfo(UtMenuInfo utMenuInfo) {
        int i = utMenuInfoDao.queryMenuNumber(utMenuInfo).getMenuNum();
        if(i==1){
            // 当前菜品订购数量为1 删除订单信息
            return utMenuInfoDao.deleteMenuInfo(utMenuInfo);
        }else {
            // 当前菜品订购数量不为1 订单数量-1
            return utMenuInfoDao.deleteMenuByUtMenuInfo(utMenuInfo);
        }
    }

    // 给厨房打印订单
    @Override
    public HashMap<String, Object> queryAllUtMenuInfo(UtMenuInfo utMenuInfo) {
        HashMap<String, Object> map = new HashMap<>();
        //第一步：创建分页对象 utMenuInfo.getPage()表示当前页，utMenuInfo.getRow()每页大小
        Page<UtMenuInfo> utMenuInfoPage = new Page<>(utMenuInfo.getPage(),utMenuInfo.getRow());

        //创建查询条件对象
        QueryWrapper<UtMenuInfo> queryWrapper = new QueryWrapper<>();
        //精确查询
        queryWrapper.eq(utMenuInfo.getMenuState()=="已结账" || utMenuInfo.getMenuState()=="已挂账", //条件
                "menu_state",  //列名
                utMenuInfo.getMenuState());   //参数值

        // 第二步: 查询分页，获取分页数据集
        Page<UtMenuInfo> pageList = utMenuInfoDao.selectPage(utMenuInfoPage,queryWrapper);

        //获取当前页的集合
        map.put("info",pageList.getRecords());
        //获取总条数
        map.put("total",pageList.getTotal());
        //获取总页数
        map.put("totalPage",pageList.getPages());
        return map;
    }

    //修改支付状态
    @Override
    public int updateUtMenuInfoState(UtMenuInfo utMenuInfo) {
        return utMenuInfoDao.updateUtMenuInfoState(utMenuInfo);
    }

}
